package com.guigu.xiaowenbo.controller;

import com.guigu.aop.OpLog;
import com.guigu.liuzhireng.pojo.Account;
import com.guigu.util.PageVo;
import com.guigu.util.service.IDService;
import com.guigu.xiaowenbo.pojo.Scell;
import com.guigu.xiaowenbo.pojo.Sgather;
import com.guigu.xiaowenbo.pojo.Sgatherdetails;
import com.guigu.xiaowenbo.service.ScellService;
import com.guigu.xiaowenbo.service.SgatherService;
import com.guigu.xiaowenbo.service.SgatherdetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

@Controller
public class SgatherController {
    @Autowired(required = false)
    SgatherService sgatherService;
    @Autowired
    IDService idService;
    @Autowired
    SgatherdetailsService sgatherdetailsService;
    @Autowired
    ScellService scellService;
    @RequestMapping("rkr")
    public ModelAndView rkr(HttpSession session){
        ModelAndView modelAndView = new ModelAndView();
        String acc = ((Account) session.getAttribute("user")).getAccount();
        modelAndView.addObject("name",acc);
        modelAndView.setViewName("xiao_wen_bo/demoew.jsp");
        return modelAndView;
    }
    @RequestMapping("insertsg")
    @ResponseBody
    @OpLog(opModule = "入库申请模块",operType = "添加入库申请",operDesc = "此方法用于添加入库申请")
    public void insertsg(@RequestBody Sgather sgather, HttpSession session){
        String strid = idService.Stock_In_ID();
        sgather.setGatherid(strid);
        int id = sgatherService.insertsg(sgather);
        session.setAttribute("sgatherID",id);
    }
    @RequestMapping("insertsgd")
    @ResponseBody
    @OpLog(opModule = "入库申请模块",operType = "添加入库申请明细",operDesc = "此方法用于添加入库申请明细")
    public int insertsgd(@RequestBody Sgatherdetails[] sgatherdetails, HttpSession session){
        Integer id = (Integer) session.getAttribute("sgatherID");
        for (Sgatherdetails sgatherdetail : sgatherdetails) {
            sgatherdetail.setParentid(id);
            sgatherdetailsService.insertsgd(sgatherdetail);
        }
        return 1;
    }
    @RequestMapping("selectsg")
    @ResponseBody
    public PageVo selectsg(@RequestBody PageVo<Sgather> sggather){
        return sgatherService.selectsg(sggather.getNum(),sggather.getSize());
    }
    @RequestMapping("updatesg")
    @OpLog(opModule = "入库申请模块",operType = "入库申请审核",operDesc = "此方法用于添加入库申请审核")
    public ModelAndView updatesg(Sgather sgather, String[] productid, String[] amount){
        Integer ab=1;
        ModelAndView modelAndView = new ModelAndView();
        if (sgather.getChecktag().equals("S001-2")){
            sgatherService.updatesg(sgather);
        }else {
            for (int i=0;i<productid.length;i++){
                Scell scell= scellService.selectByPrimaryKeyfhcxid(productid[i]);
                Integer am = scell.getAmount();
                Integer mam= scell.getMaxcapacityamount();
                Integer amo=mam-am;
                System.out.println(amo);
                System.out.println(amount[i]);
                if (amo<Integer.parseInt(amount[i])){
                    ab=2;
                }
            }
            System.out.println(ab);
            if (ab==1){
                sgatherService.updatesg(sgather);
            }else{
                modelAndView.addObject("errors","申请数量大于最大存储量");
                modelAndView.setViewName("selectsgd?id="+sgather.getId());
                return modelAndView;
            }
        }
        modelAndView.setViewName("xiao_wen_bo/selectByPrimaryKeyfhrksh.jsp");
        return modelAndView;
    }

    @RequestMapping("selectsga")
    @ResponseBody
    public PageVo selectsga(@RequestBody PageVo<Sgather> sgatherPageVo){
        return sgatherService.selectsga(sgatherPageVo.getNum(),sgatherPageVo.getSize());
    }
    @RequestMapping("selectsgdd")
    @ResponseBody
    public PageVo selectsgdd(@RequestBody PageVo<Sgather> sgatherPageVo){
        return sgatherService.selectsgdd(sgatherPageVo.getNum(),sgatherPageVo.getSize());
    }
    @RequestMapping("selectsgrs")
    @ResponseBody
    public PageVo selectsgrs(@RequestBody PageVo<Sgather> sgatherPageVo){
        return sgatherService.selectsgrs(sgatherPageVo.getNum(),sgatherPageVo.getSize());
    }
    @RequestMapping("updatesgre")
    @OpLog(opModule = "入库管理模块",operType = "入库管理登记",operDesc = "此方法用于添加入库管理登记")
    public ModelAndView updatesgre(String register, String gatherid, String[] productid, Integer[] parentid, Integer[] gatheredamount, String[] amount, Integer[] gatheredamounts){
        ModelAndView modelAndView = new ModelAndView();
        for (int i=0;i<parentid.length;i++){
            Integer a =Integer.parseInt(amount[i])-gatheredamount[i];
            if (a<gatheredamounts[i]){
                modelAndView.addObject("error","入库数量大于当前可存放数量或应入库数量");
                modelAndView.setViewName("selectsgdrs?parentid="+parentid[i] );
                return modelAndView;
            }else {
                sgatherdetailsService.updatesgdrses(parentid[i],productid[i],gatheredamounts[i]);
            }
        }
        sgatherService.updatesgre(gatherid,register);
        modelAndView.setViewName("xiao_wen_bo/selectsgrs.jsp");
        return modelAndView;
    }
    @RequestMapping("selectsgrssh")
    @ResponseBody
    public PageVo selectsgrssh(@RequestBody PageVo<Sgather> sgatherPageVo){
        return sgatherService.selectsgrssh(sgatherPageVo.getNum(),sgatherPageVo.getSize());
    }
    @RequestMapping("updatesgresh")
    @OpLog(opModule = "入库管理模块",operType = "入库管理登记审核",operDesc = "此方法用于入库管理登记审核")
    public String updatesgresh(Sgather sgather, Integer[] parentid, String[] productid, Integer[] gatheredamount, Integer[] gatheredamounts, Integer[] amount){
        Integer num=1;
        Integer amsum = 0;
        for (int i=0;i<parentid.length;i++){
            amsum+=gatheredamounts[i];
            sgatherdetailsService.updatesgdrs(parentid[i],productid[i],gatheredamounts[i]);
            sgatherdetailsService.updatesgdrses(parentid[i],productid[i],0);
            scellService.updatesc(productid[i],gatheredamounts[i]);
            System.out.println("gatheredamounts+"+gatheredamounts[i]);
            System.out.println("gatheredamount+"+gatheredamount[i]);
            Integer ga = amount[i]-gatheredamount[i];
            if (gatheredamounts[i]<ga){
                num+=1;
            }
        }
        sgather.setGatheredamountsum(amsum);
        System.out.println(num);
        if (num==1){
            sgatherService.updatesgresh(sgather);
        }else {
            sgatherService.updatesgreshs(sgather);
        }
        return "xiao_wen_bo/selectsgrssh.jsp";
    }
    @RequestMapping("selectsgre")
    @ResponseBody
    public PageVo selectsgre(@RequestBody PageVo<Sgather> sgatherPageVo){
        return sgatherService.selectsgre(sgatherPageVo.getNum(),sgatherPageVo.getSize());
    }
}
